package io.renren.modules.pachong.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.pachong.Funny;
import io.renren.modules.pachong.dao.FunnyDao;
import io.renren.modules.pachong.service.FunnyService;
import io.renren.modules.pachong.util.HtmlParser;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author Peter
 * @date 2018-8-7
 */
@Service("funnyService")
public class FunnyServiceImpl extends ServiceImpl<FunnyDao, Funny> implements FunnyService {

    @Override
    public void addNewFunnyData(InputStream in,Integer type) throws FileNotFoundException {
        String str = HtmlParser.getXmlStr(in);
        List<Funny> list = HtmlParser.funny(str);
        List<Funny> r = new ArrayList<>(list.size());
        for(int i=list.size()-1;i>=0;i--){
            Funny f = list.get(i);
            f.setType(type);
            r.add(f);
        }
//        Collections.reverse(list);
        super.insertBatch(r);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        EntityWrapper<Funny> ew = new EntityWrapper<>();
        ew.orderBy("id", false);
        ew.eq("type",params.get("type"));
        Page<Funny> page = super.selectPage(new Query<Funny>(params).getPage(), ew);
        return new PageUtils(page);
    }



}
